﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ComunesMGI.DataTypes;

namespace StreamTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void Attach(string nom, DataSistema sistema , DataIncidente inc )
        {
            ServiceReference1.MGIIncidentesWSClient client = new ServiceReference1.MGIIncidentesWSClient();
        }
        private void UpLoadFile(string fileName)
        {
            try
            {
                string getFile = System.IO.Path.GetFileName(fileName);
                ServiceReference1.MGIIncidentesWSClient client = new ServiceReference1.MGIIncidentesWSClient();
                
                //ServiceIncidentesClient client = new ServiceIncidentesClient();
                FileInfo fInfo = new FileInfo(fileName);
                long noBytes = fInfo.Length;
                double dLen = Convert.ToDouble(fInfo.Length / 1000000);
                if (dLen < 4)
                {
                    FileStream fStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    string sTmp = client.UploadFile(fStream);

                    fStream.Close();
                    fStream.Dispose();

                    DataSistema sis = new DataSistema("IC-Banking","http://pis2008/pis2008/infocorp/" );
                    DataIncidente inc = new DataIncidente("1","");
                    client.adjuntarArchivoIncidente(fileName.Substring(fileName.LastIndexOf("\\")),sis,inc,sTmp);
                    // this will always say OK unless an error occurs,
                    // if an error occurs, the service returns the error message
                    MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
                }
                else
                {
                    // Display message if the file was too large to upload
                    MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
                }

            }
            catch (Exception ex)
            {
                // display an error message to the user
                MessageBox.Show(ex.Message.ToString(), "Upload Error");
            }

        }

        private void btnBrowse_Click_1(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open File";
            openFileDialog1.Filter = "All Files | *.*";
            openFileDialog1.FileName = "";
            try
            {
                openFileDialog1.InitialDirectory = "C:\\Temp";
            }
            catch
            {

            }

            openFileDialog1.ShowDialog();
            if (openFileDialog1.FileName == "")
                return;
            else
                textBox1.Text = openFileDialog1.FileName;
        }

        private void btnUpload_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text != string.Empty)
                UpLoadFile(textBox1.Text);

            else
                MessageBox.Show("You must select a Valid File Name First");

        }



    }
}
